﻿
/// <reference path="../jquery-1.6.2-vsdoc.js" />

(function ($) {
    $.extend({
        FlexExplorer: {
            baseUrl: '/File/Manager/',
            lastPathCookie: '__Flex_LAST_PATH',
            args: null,
            Flash: {
                swf: '/scripts/BasicFM.swf',
                targetId: '_flashPanel',
                attributes: {},
                params: {
                    wmode: 'transparent',
                    quality: 'best',
                    width: "100%",
                    height: "100%"
                },
                flashvars: {
                    focusPath: '',
                    sessionId: '',
                    returnFull: 1
                },
                tmp: '<embed src="{0}" width="{1}" height="{2}" flashvars="{3}" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" autostart="true" autoplay="true" wmode="transparent"></embed>'
            },

            String: {
                trim: function () {
                    return this.replace(/^\s+|\s+$/g, '');
                },

                //if( $.StringEx.isNullOrEmpty('') ) alert('Empty string');
                isNullOrEmpty: function (value) {
                    if (value) {
                        if (typeof (value) == 'string') {
                            if (value.length > 0)
                                return false;
                        }
                        if (value != null)
                            return false;
                    }
                    return true;

                },

                //alert( $.StringEx.format("Hello {0}.","Nasa8x") );
                format: function (format) {
                    var result = format;
                    for (var i = 1; i < arguments.length; i++) {
                        result = result.replace(new RegExp('\\{' + (i - 1) + '\\}', 'g'), arguments[i]);
                    }
                    return result;
                },
                startsWith: function (prefix, ignoreCase) {
                    if (!prefix) return false;
                    if (prefix.length > this.length) return false;
                    if (ignoreCase) {
                        if (ignoreCase == true) {
                            return (this.substr(0, prefix.length).toUpperCase() == prefix.toUpperCase());
                        }
                    }
                    return (this.substr(0, prefix.length) === prefix);
                },
                endsWith: function (suffix, ignoreCase) {
                    if (!suffix) return false;
                    if (suffix.length > this.length) return false;
                    if (ignoreCase) {
                        if (ignoreCase == true) {
                            return (this.substr(this.length - suffix.length).toUpperCase() == suffix.toUpperCase());
                        }
                    }
                    return (this.substr(this.length - suffix.length) === suffix);
                }
            },

            objectToArgs: function (o) {
                var type = typeof (o);
                var a = [];
                if (type == "object") {
                    for (var key in o) {
                        if (typeof (o[key]) == "object") {
                            this.objectToArgs(o[key]);
                        }
                        else {
                            a.push(key + '=' + encodeURIComponent(o[key]));
                        }

                    }
                    return a.join('&');
                }
                return '';
            },

            getParentWin: function () {
                var w = opener || parent || top;
                return w != window ? w : null;
            },

            getWin: function () {
                var w = opener || parent || top;
                return w != window ? w : null;
            },
            getWindowArg: function (c, b) {
                var a = this.params[c];
                return $.is(a) ? a : b;
            },

            queryString: function (key) {
                key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                var regex = new RegExp("[\\?&]" + key + "=([^&#]*)", 'ig');
                var qs = regex.exec(window.location.href);
                if (qs == null)
                    return '';
                else
                    return qs[1];

            },

            rememberLastPath: function (_val) {
                $.Cookie.add(this.lastPathCookie, _val, { days: 0.5 });
            },

            convertFromUnixTimestamp: function (s) {
                var _d = new Date();
                _d.setTime(s * 1000);

                return _d;
            },

            convertToUnixTimestamp: function (date) {
                return date.getTime() / 1000;
            },

            getFilePath: function (_file) {
                return _file.substring(0, _file.lastIndexOf('/'));
            },

            getFileName: function (_file) {
                return _file.substring(0, _file.lastIndexOf('.'));
            },

            getFocusPath: function () {

                var _target = this.queryString('target');
                if (!this.String.isNullOrEmpty(_target)) {
                    if (_target.indexOf("/") != -1) {

                        return this.getFilePath(_target);

                    } else if (_target.indexOf(".") != -1) {

                        var _date = this.convertFromUnixTimestamp(this.getFileName(_target));

                        if (_date) {
                            return this.dateFormat(_date, "yyyy/mm/dd");
                        }

                    }
                }

                var _folder = $.Cookie.get(this.lastPathCookie);
                if (!this.String.isNullOrEmpty(_folder)) {
                    return _folder;
                };
                return '';

            },

            showDialog: function (url, opts) {
                var w = opts && opts.width > 0 ? opts.width : screen.width * 0.8;
                var h = opts && opts.height > 0 ? opts.height : screen.height * 0.8;
                window.showModalDialog(url, opts, 'dialogWidth=' + w + ',dialogHeight=' + h + ',center=1');
            },


            open: function (o) {

                this.showDialog(this.baseUrl, o);

            },

            insert: function (_val) {

                var url = _val;
                var win = this.getWin();

                var args = this.args;

                if (args) {

                    if (args.returnId) {
                        win.document.getElementById(args.returnId).value = url;

                    }
                    else if (args.callback) {

                        args.files = _val;
                        args.callback.call(win, args);

                    }

                    self.close();

                    return;

                }
                else if (tinymce) { // Plugin TinyMce 

                    var _ed = tinyMCEPopup.getWindowArg("editor");
                    win = tinyMCEPopup.getWindowArg("window");

                    var returnId = tinyMCEPopup.getWindowArg("input");
                    if (returnId != null) {
                        // insert information now
                        win.document.getElementById(returnId).value = url;

                    }
                    else {

                        var _imgs = _val.split(';');
                        $.each(_imgs, function (index, value) {
                            _ed.execCommand('mceInsertContent', false, '<img src="' + value + '" /><br/><br/>');
                        });

                    };

                    if (typeof (win.ImageDialog) != 'undefined') {
                        // for image browsers: update image dimensions
                        if (win.ImageDialog.getImageData) {
                            win.ImageDialog.getImageData();
                        }
                        // show preview image
                        if (win.ImageDialog.showPreviewImage) {
                            win.ImageDialog.showPreviewImage(url);
                        }
                    };

                    // close popup window
                    tinyMCEPopup.close();
                }

                else if (navigator.appName.indexOf('Microsoft') != -1) {
                    window.returnValue = url;
                    self.close();
                }

                return;

            },

            embed: function (w, h, fvars) {
                var f = this.Flash;
                var flashvars = $.extend({}, f.flashvars, fvars || {});
                var width = w ? w : $(window).width();
                var height = h ? h : $(window).height();
                var v = this.objectToArgs(flashvars);
                var html = $(this.String.format(f.tmp, f.swf, width, height, v)).attr("id", f.targetId + '_embed');
                $("#" + f.targetId).html(html);

                $(window).bind('resize', $.proxy(function () {
                    $("#" + f.targetId + '_embed').width($(window).width()).height($(window).height());

                }, this));

            },

            onLoad: function () {

                var returnId = this.queryString("returnId");
                if (returnId != '')
                    this.args.returnId = returnId;

                this.Flash.flashvars.focusPath = this.getFocusPath();
                this.Flash.flashvars.returnFull = this.queryString("returnFull");

                if (window.dialogArguments)
                    this.args = window.dialogArguments;

                this.embed();

            }

            // add more function

        }// end 
    });   // end extend

})(jQuery);
